package com.ipzoe.common.service;

import com.github.pagehelper.PageHelper;
import com.ipzoe.common.bean.entity.Admin;
import com.ipzoe.common.bean.entity.Role;
import com.ipzoe.common.bean.entity.RoleAdminMenu;
import com.ipzoe.common.bean.response.Code;
import com.ipzoe.common.bean.response.PageBean;
import com.ipzoe.common.bean.response.Response;
import com.ipzoe.common.repository.RoleRepository;
import com.ipzoe.common.util.DefaultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 角色Service
 * <p>
 * Created 2018/01/19.
 *
 * @author pfzhang
 */
@Service
@Transactional(rollbackFor = RuntimeException.class)
public class RoleService extends DefaultService<Role> {

    private RoleRepository roleRepository;

    @Resource
    public void setRoleRepository(RoleRepository roleRepository) {
        this.roleRepository = roleRepository;
        super.setDefaultMapper(roleRepository);
    }

    @Autowired
    private RoleAdminMenuService roleAdminMenuService;

    /**
     * 查询角色集合
     *
     * @param key  关键词,根据角色名称查询
     * @param page 当前页
     * @param size 页面大小
     * @return 角色集合分页对象
     */
    public Response<PageBean<Role>> list(String key, int page, int size) {
        PageHelper.startPage(page, size);
        List<Role> list = roleRepository.search("%" + key + "%");
        return Response.ok(new PageBean<>(list));
    }

    /**
     * 查询所有未删除角色集合
     *
     * @return 角色集合
     */
    public Response<List<Role>> selectAllNotDeleted() {
        List<Role> roleList = roleRepository.search("%%");
        return Response.ok(roleList);
    }

    /**
     * 添加/编辑角色
     *
     * @param admin 管理员
     * @param role  角色对象,没有ID为添加,否则为编辑
     * @return 接口响应对象
     */
    public Response saveOrUpdateRole(Admin admin, Role role) {
        Role oldRole = roleRepository.findByName(role.getName());

        Long roleId = role.getId();
        Role newRole;
        if (null == roleId) {
            //新增角色
            //判断角色名是否存在
            if (null != oldRole) {
                return Response.error(Code.ADMIN_ROLE_NAME_EXIST);
            }
            //添加角色
            newRole = new Role();
        } else {
            //编辑角色
            if (null != oldRole && !oldRole.getId().equals(roleId)) {
                return Response.error(Code.ADMIN_ROLE_NAME_EXIST);
            }
            //无法编辑超级管理员
            if (1L == roleId) {
                return Response.error(Code.UNKNOWN);
            }
            //编辑角色
            newRole = selectById(roleId);
            if (null == newRole) {
                return Response.error(Code.ADMIN_ROLE_NOT_EXIST);
            }
        }
        newRole.setName(role.getName());
        newRole.setDescription(role.getDescription());
        if (null == roleId) {
            insertSelective(admin.getUsername(), newRole);
        } else {
            updateSelective(admin.getUsername(), newRole);
        }

        //如果是更新,先删除之前的角色菜单绑定对象
        if (null != roleId) {
            roleAdminMenuService.deletedByRoleId(roleId);
        }

        //保存角色菜单绑定对象
        List<Long> menuIdList = role.getMenuIdList();
        if (null != menuIdList && menuIdList.size() > 0) {
            for (Long menuId : menuIdList) {
                RoleAdminMenu roleAdminMenu = new RoleAdminMenu();
                roleAdminMenu.setAdminMenuId(menuId);
                roleAdminMenu.setRoleId(newRole.getId());
                roleAdminMenuService.insertSelective(admin.getUsername(), roleAdminMenu);
            }
        }

        //查询该角色是否绑定了首页菜单,若没有,绑定
        RoleAdminMenu roleAdminMenu = roleAdminMenuService.selectByRoleIdAndMenuId(newRole.getId(), 1L);
        if (null == roleAdminMenu) {
            RoleAdminMenu indexMenu = new RoleAdminMenu();
            indexMenu.setRoleId(newRole.getId());
            indexMenu.setAdminMenuId(1L);
            roleAdminMenuService.insertSelective(admin.getUsername(), indexMenu);
        }

        return Response.ok();
    }

    /**
     * 根据角色的id获取角色的详情
     *
     * @param roleId 角色ID
     * @return 角色对象
     */
    public Role getRoleDetailById(Long roleId) {
        // 查询角色的基本信息
        Role role = selectById(roleId);
        // 获取该角色的菜单列表的ID
        List<Long> menuList = roleAdminMenuService.getIdListByRoleId(roleId);
        role.setMenuIdList(menuList);
        return role;
    }
}
